package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.CarStorageActivity;
import com.novellotus.iov.carvice.activity.OperationActivity;
import com.novellotus.iov.carvice.activity.TeamManagementActivity;
import com.novellotus.iov.carvice.bean.RxEinfos;
import com.novellotus.iov.carvice.utils.MyViewUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/12/26.
 */

public class WorkFragmentAdapter extends RecyclerView.Adapter {

    private Context context;
    private RxEinfos rxEinfos;

    public WorkFragmentAdapter(Context context, RxEinfos rxEinfos) {
        this.context = context;
        this.rxEinfos = rxEinfos;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_work_fragment_picture, null);
            return new PictureHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_work_role, null);
            return new RoleHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (position == 0) {
            initPicture((PictureHolder) holder);
        } else {
            initRole((RoleHolder) holder, position - 1);
        }

    }

    private void initPicture(PictureHolder pictureHolder) {
        //    Glide.with(context).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514022486483&di=ca5127fb293453f7f3192c61117d08a0&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F2934349b033b5bb59c41e3d33dd3d539b600bc0d.jpg").into(pictureHolder.img_banner);
    }

    private void initRole(RoleHolder roleHolder, final int position) {
        roleHolder.recyclerView.setLayoutManager(MyViewUtils.gridLayoutManager(context, 4));
        roleHolder.recyclerView.setAdapter(new WorkFragmentRoleItemAdapter(rxEinfos.getRoles().get(position).getMenus(), rxEinfos.getECode() + "", rxEinfos.getRoles().get(position).getRoleCode()));
        roleHolder.tv_role.setText(rxEinfos.getRoles().get(position).getRoleName());
        roleHolder.tv_team.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, TeamManagementActivity.class);
                intent.putExtra("eCode", rxEinfos.getECode() + "");
                intent.putExtra("rCode", rxEinfos.getRoles().get(position).getRoleCode());
                context.startActivity(intent);
            }
        });
    }


    /**
     * @param rxEinfos 刷新企业角色
     */
    public void updateCompany(RxEinfos rxEinfos) {
        this.rxEinfos = rxEinfos;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return rxEinfos.getRoles().size() + 1;
    }

    class RoleHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.tv_role)
        TextView tv_role;
        @BindView(R.id.tv_team)
        TextView tv_team;

        RoleHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class PictureHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_banner)
        ImageView img_banner;

        PictureHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
